package com.yoso.shop.infrastructure.init;

import cn.hutool.core.io.FileUtil;
import com.yoso.shop.infrastructure.config.InitConfig;
import com.yoso.shop.infrastructure.constants.commodity.StaticResourcesPath;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Service;

import java.io.File;

/**
 * @author shizejin
 * @date 2022/2/23
 * @description: 静态资源目录初始化
 */
@Service
@Data
@Slf4j
public class StaticResourceFolder implements ApplicationRunner {
    @Autowired
    private InitConfig initConfig;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        String staticFolderPath = initConfig.getStaticFolderPath();
        if (!StringUtils.endsWith(staticFolderPath, File.separator)) {
            staticFolderPath = staticFolderPath + File.separator;
        }
        log.info("初始化静态资源目录{}", staticFolderPath);
//        根目录
        File staticRootDir = FileUtil.mkdir(staticFolderPath);
//        头像目录
        File iconDir = FileUtil.mkdir(staticFolderPath + StaticResourcesPath.ICON_PATH);
//        轮播图目录
        File lunboDir = FileUtil.mkdir(staticFolderPath + StaticResourcesPath.LUNBO_PATH);
//        商品照片目录
        File commodityDir = FileUtil.mkdir(staticFolderPath + StaticResourcesPath.COMMODITY_PATH);
        if (ObjectUtils.anyNull(staticRootDir, iconDir, lunboDir, commodityDir)) {
            log.warn("静态资源目录初始化失败,将导致静态资源加载失败！！！");
        }
    }
}
